/**
 * Created by zpc on 2017/12/13.
 */
$(function () {
    getProduct();
    $(".addShopCar").click(function () {
        var amount = parseInt($(".amount_box input[type='text']").val());
        var productEntityId = $(".color_box .hover1").attr("data-id");
        var postData = {amount: amount, productEntityId: productEntityId}
        $.ajax({
            type: "post",
            url: "/shoppingCart/modifyShoppingCart.html",
            dataType: "json",
            async: false,
            data: postData,
            success: function (res) {
                console.log(res);
                if (res.code != 1) {
                    alert(res.msg);
                    return;
                }
                alert("购物车添加成功！");

            }

        });

    });
    $(".amount_box a:eq(0)").click(function () {
        var amount = parseInt($(".amount_box input[type='text']").val());
        amount -= 1;
        if (amount < 1) {
            alert("数量最少是1！");
            return;
        }
        $(".amount_box  input[type='text']").val(amount);
    });
    $(".amount_box a:eq(1)").click(function () {
        var amount = parseInt($(".amount_box input[type='text']").val());
        amount += 1;
        $(".amount_box  input[type='text']").val(amount);
    });


    var mySwiper1 = new Swiper('.banner_box .swiper-container', {
        autoplay: 2000, //可选选项，自动滑动
        pagination: '.banner_box .swiper-pagination', //分页器
        speed: 2000,
        loop: true, //环路
        observer: true,
        observeParents: true,
        autoplayDisableOnInteraction: false, //点击之后继续播放轮播图
        pagination: '.swiper-pagination',//小圆点点击事件
        paginationClickable: true, //小圆点点击事件
        slidesPerView: 1,//显示的数量
        slidesPerGroup: 1,//显示一次跳动几张

    })
    $('.banner_box').hover(function () {
        mySwiper1.stopAutoplay();
    }, function () {
        mySwiper1.startAutoplay();

    });

    $(".sub_button").click(function () {
        $(".purchase_details").show();
        $(".background_box").show();
    });
    $(".purchase_details .top .esc").click(function () {
        $(this).parents(".purchase_details").hide();
        $(this).parents(".purchase_details").siblings(".background_box").hide();
    });
    $(".background_box").click(function () {
        $(this).hide();
        $(this).siblings(".purchase_details").hide();
    })

    $(".buy").click(function () {

        var userId = $("#userId").val();
        if (userId == undefined || userId == '') {
            alert("用户没有登录！");
            return;
        }
        //orderPriceAmount();

        topay();
    });


});

function topay() {
    var amount = parseInt($(".amount_box input[type='text']").val());

    var productJsons = [];
    var productEntityId = $(".color_box .hover1").attr("data-id");
    if (productEntityId != '' && productEntityId != undefined) {
        productJson = {productEntityId: productEntityId, buyNum: amount};
        productJsons.push(productJson);
    }
    console.log(productJsons);
    var postData = {productJsons: JSON.stringify(productJsons), source: 1};
    var activityId = $("#activityId").val();
    if (activityId != '' && activityId != undefined && activityId != null) {
        postData["activityId"] = activityId;
    }


    createOrder(postData);
}
function orderPriceAmount() {
    var postData = {};
    $.ajax({
        type: "post",
        url: "/order/getOrderPriceAmount.html",
        dataType: "json",
        async: false,
        data: postData,
        success: function (res) {
            console.log(res);
            if (res.code != 1) {
                alert(res.msg);
                return;
            }
      /*      if (res.data > 100000) {
                if (confirm("你本周的消费已经超过1000，是否继续？") == false) {
                    return;

                }

                //  location.href = "/pay/" + res.data.orderId + ".html";
            }*/
            topay();
        }
    });
}
function createOrder(postData) {
console.log(postData);
    $.ajax({
        type: "post",
        url: "/order/createOrder.html",
        dataType: "json",
        async: false,
        data: postData,
        success: function (res) {
            console.log(res);
            if (res.code != 1) {
                alert(res.msg);
                return;
            }
            //  alert("兑换成功！");
             location.href = "/pay/" + res.data.orderId + ".html";
        }

    });
}
function getProduct() {
    var productId = $("#productId").val();
    var postData = {productId: productId}
    $.ajax({
        type: "post",
        url: "/product/getProduct.html",
        dataType: "json",
        async: false,
        data: postData,
        success: function (res) {
            console.log(res);
            if (res.code != 1) {
                alert(res.msg);
                return;
            }
            var data = res.data;
            $(".introduce h3").html(data.productName);
            $(".parameter_img").html(data.productIntroduction);
            var colorHtml = "";


            for (var i = 0; i < data.productEntityList.length; i++) {
                if (data.productEntityList[i].status != 0) {
                    colorHtml += ' <span ';
                    if (data.productEntityList[i].status == 1) {
                        colorHtml += ' onclick="setColor(' + data.productEntityList[i].productEntityId + ',' + i + ')" data-id="' + data.productEntityList[i].productEntityId + '"';
                    }
                    colorHtml += '>' + data.productEntityList[i].productEntityColour + '</span>';
                }
            }
            $(".color_box").html(colorHtml);
            setColor(data.productEntityList[0].productEntityId, 0);
        }

    });
}
function setColor(productEntityId, index) {
    var postData = {productEntityId: productEntityId}
    var activityId = $("#activityId").val();
    if (activityId != '' && activityId != undefined && activityId != null) {
        postData["activityId"] = activityId;
    }


    $.ajax({
        type: "post",
        url: "/productEntity/getProductEntity.html",
        dataType: "json",
        async: false,
        data: postData,
        success: function (res) {
            console.log(res);
            if (res.code != 1) {
                alert(res.msg);
                return;
            }
            var data = res.data;
            var price = "";
            if (data.price > 0) {
                price = data.price / 100 + "元"
            }
            $(".price_past").find("a").html(price);
            $(".price_now").html(data.realPrice / 100);
            // $(".price_details h2").html(data.realPrice/100);
            $("#price").val(data.realPrice);
            $(".color_box span").removeClass();
            $(".color_box span").eq(index).addClass("hover1");
            //  $(".purchase_details_2 div").html(data.productEntityIntroduction);
            $(".slogan").html(data.productEntityIntroduction);

            getProductPackage(data.productEntityId);
            setPic(data.productEntityId);
        }

    });

}
function setPic(productEntityId) {
    var postData = {resourceRelationId: productEntityId, resourceRelationType: 2}
    $.ajax({
        type: "post",
        url: "/resource/getResourceList.html",
        dataType: "json",
        async: false,
        data: postData,
        success: function (res) {
            console.log(res);
            if (res.code != 1) {
                alert(res.msg);
                return;
            }
            var ul1 = $("#ul1");

            var data = res.data;
            var html = "";
            var pic = "";
            $("#ul1").html();
            if (data.length > 0) {
                for (var i = 0; i < data.length; i++) {
                    //    html += '<li class="lili1"><img class="lili_img" src="' + picPath + data[i].resourceUrl + '" alt="" /></li>';
                    html += ' <div class="swiper-slide"><img src="' + picPath + data[i].resourceUrl + '" alt=""></div>';

                }
                pic = picPath + data[0].resourceUrl;

            }
            $(".img_box img").attr("src", picPath + data[0].resourceUrl);
            //  $("#ul1").html(html);
            $(".swiper-wrapper").html(html);

            $("#weixin_main_left1").find("img").attr('src', pic);
            var dd = "url(" + pic + ") no-repeat";
            $("#weixin_main_left2").css({"background": dd});
            $("#weixin_main_left2").css({"background-size": "200%"});


            var li1 = $(".lili1");
            li1.click(function () {
                var img1 = $(this).find(".lili_img").attr("src");
                $("#weixin_main_left1").find(".left_img").attr("src", img1);
                var img2 = "url(" + img1 + ") no-repeat";
                $("#weixin_main_left2").css({"background": img2});
                $("#weixin_main_left2").css({"background-size": "200%"});

            });
        }
    });
}

function getProductPackage(productEntityId) {
    var postData = {productEntityId: productEntityId}
    $.ajax({
        type: "post",
        url: "/product/getProductPackage.html",
        dataType: "json",
        async: false,
        data: postData,
        success: function (res) {
            console.log(res);
            if (res.code != 1) {
                alert(res.msg);
                return;
            }
            var data = res.data;
            var packageHtml = '';
            var realPrice = 0;


            if (data.length > 0) {

                packageHtml = '<h3 class="title1">套餐类型</h3><div class="package_box"> ';
                for (var i = 0; i < data.length; i++) {
                    packageHtml += '  <span class="choice" data-id="' + data[i].packageId + '"  onclick="setPackage(' + data[i].realPrice + ',' + i + ')">' + data[i].packageName + '</span>';
                }

                realPrice = data[0].realPrice;
                packageHtml += ' </div>';
                console.log("data.length===>>>0===" + data.length)

            }
            $(".package").html(packageHtml);
            setPackage(realPrice, 0);
        }

    });
}
function setPackage(packagePrice, index) {
    var price = parseInt($("#price").val());
    price += parseInt(packagePrice);
    $(".price_details h2").html(price / 100 + "元");
    $(".package_box span").removeClass();
    $(".package_box span").eq(index).addClass("hover1");
}